﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TaiwuZD
{
    /// <summary>
    /// ActorSkillCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class ActorSkillCtrl : UserControl
    {
        public static CollectionViewSource TotalSkillSource { get; set; } = new CollectionViewSource();
        public static CollectionViewSource OwnedSkillSource { get; set; } = new CollectionViewSource();

        public static RoutedCommand ForgetSkillCommand { get; set; } = new RoutedCommand();
        public static RoutedCommand LearnSkillCommand { get; set; } = new RoutedCommand();


        public ActorSkillCtrl()
        {
            InitializeComponent();

            TotalSkillSource.Source = GameAssets.TwSkills.Values;
            TotalSkillSource.Filter += TotalSkillSource_Filter;

            OwnedSkillSource.Source = Global.Inst.CurrentRole?.OwnedSkills;
            OwnedSkillSource.Filter += TotalSkillSource_Filter; 


            Global.Inst.PropertyChanged += Inst_PropertyChanged;
        }
        private void TotalSkillSource_Filter(object sender, FilterEventArgs e)
        {
            var tid = ((KeyValuePair<int, string>)lvSkillType.SelectedItem).Key; 
            var gf = (e.Item as TwSkill);

            if (tid != gf.TypeId)
                e.Accepted = false; 
            else
                e.Accepted = true;
        }
        private void Inst_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Global.CurrentRole))
            {
                OwnedSkillSource.Source = Global.Inst.CurrentRole.OwnedSkills;
            }
        }

        private void lvSkillType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TotalSkillSource?.View?.Refresh();
            OwnedSkillSource?.View?.Refresh();
        }

        private void ForgetSkillCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = lvSkillOwned?.SelectedItem != null;
        }

        private void ForgetSkillCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var gf = lvSkillOwned.SelectedItem as TwSkill;
            Global.Inst.CurrentRole.OwnedSkills.Remove(gf);
        }

        private void LearnSkillCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Global.Inst.IsSFLoaded && lvSkillGallay?.SelectedItem != null;
        }

        private void LearnSkillCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var gf = lvSkillGallay.SelectedItem as TwSkill;

            if (gf != null)
            {
                foreach (var item in lvSkillGallay.SelectedItems)
                {
                    gf = item as TwSkill;
                    var ngf = gf.Clone() as TwSkill;
                    ngf.Proficiency = 100;
                    ngf.Degree = 10; 
                    Global.Inst.CurrentRole.OwnedSkills.Add(ngf);
                }
                lvSkillGallay.SelectedItems.Clear();
            }
        }
    }
}
